package org.linlinjava.litemall.admin.shiro;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.linlinjava.litemall.admin.service.CaptchaCodeManager;
import org.linlinjava.litemall.db.dao.LitemallAdminMapper;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.service.LitemallPermissionService;
import org.linlinjava.litemall.db.service.LitemallRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;
@Service
public class AdminMobOremailRealm extends AuthorizingRealm {

    @Autowired
    private LitemallAdminMapper litemallAdminMapper;

    @Autowired
    private LitemallRoleService roleService;
    @Autowired
    private LitemallPermissionService permissionService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }
        LitemallAdmin admin = (LitemallAdmin) getAvailablePrincipal(principals);
        Integer[] roleIds = admin.getRoleIds();
        Set<String> roles = roleService.queryByIds(roleIds);
        Set<String> permissions = permissionService.queryByRoleIds(roleIds);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(roles);
        info.setStringPermissions(permissions);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken  adminMobOremailToken = (UsernamePasswordToken)authenticationToken;
        String mobOremail = adminMobOremailToken.getUsername();
        String authCode = new String(adminMobOremailToken.getPassword());

        //去数据库根据手机号查询用户信息
        LitemallAdmin litemallAdmin = litemallAdminMapper.selectOne(Wrappers.lambdaQuery(LitemallAdmin.class)
                .eq(LitemallAdmin::getMobile, mobOremail)
                .eq(LitemallAdmin::getDeleted, 0)
                .or()
                .eq(LitemallAdmin::getEmail, mobOremail)
                .eq(LitemallAdmin::getDeleted, 0)
        );
        //缓存里的验证码
        String verificationCode = CaptchaCodeManager.getCachedCaptcha(mobOremail+"2");
        if (StrUtil.isEmpty(verificationCode)) {
            throw new ServiceException("网络错误");
        }
        //比对手机号
        if (!verificationCode.equals(authCode)) {
            throw new ServiceException("验证码不正确");
        }
        if (litemallAdmin == null) {
            throw new UnknownAccountException();
        }
        if (litemallAdmin.getDeleted()) {
            throw new LockedAccountException();
        }
        return new SimpleAuthenticationInfo(litemallAdmin,authCode,getName());
    }
}
